
%{
#define YYERROR_VERBOSE
#include "libpatches.h"
#include <stdio.h>
#include <stdlib.h>
%}

%union {
	char * string;
}

%token <string> TOK_WORD TOK_DECIMAL TOK_INTEGER
%token TOK_MODULE TOK_INIT TOK_PATCH TOK_SAMPLES TOK_SECONDS TOK_SR TOK_COLON TOK_END TOK_ERROR
%type <string> value stream number

%%

program:	command
	|	program command
	;

command:	TOK_END
	|	module
	|	init
	|	patch
	|	sr
	|	seconds
	|	samples
	;

module:		TOK_MODULE TOK_WORD TOK_WORD TOK_END {
			safe_module($2, $3);
			/* fprintf(stderr, "module '%s', '%s'\n", $2, $3); */
		}
	;

init:		TOK_INIT TOK_WORD TOK_WORD value TOK_END {
			safe_init($2, $3, $4);
			/* fprintf(stderr, "init '%s' '%s' '%s'\n", $2, $3, $4); */
		}
	;

value:		TOK_WORD
	|	TOK_DECIMAL
	|	TOK_INTEGER
	;

patch:		TOK_PATCH TOK_WORD TOK_COLON stream TOK_WORD TOK_COLON stream TOK_END {
			safe_patch($2, $4, $5, $7);
			/* fprintf(stderr, "patch %s:%s <- %s:%s\n", $2, $4, $5, $7); */
		}
	|	TOK_PATCH TOK_WORD TOK_COLON stream number TOK_END {
			safe_patch_constant($2, $4, $5);
			/* fprintf(stderr, "patch %s:%s <- %s\n", $2, $4, $5); */
		}
	|	TOK_PATCH TOK_WORD TOK_WORD TOK_END {
			safe_patch_all($2, $3);
			/* fprintf(stderr, "patch %s:* <- %s:*\n", $2, $3); */
		}
	;

seconds:	TOK_SECONDS number TOK_END {
			safe_set_seconds($2);
			/* fprintf(stderr, "seconds = %s\n", $2); */
		}
	;

samples:	TOK_SAMPLES TOK_INTEGER TOK_END {
			safe_set_samples($2);
			/* fprintf(stderr, "samples = %s\n", $2); */
		}
	;


sr:		TOK_SR TOK_INTEGER TOK_END {
			safe_set_sampling_rate($2);
			/* fprintf(stderr, "sampling rate = %s\n", $2); */
		}
	;

stream:		TOK_WORD
	|	TOK_INTEGER
	;

number:		TOK_DECIMAL
	|	TOK_INTEGER
	;

%%

yyerror(char * s) {
	fprintf(stderr, "%s\n", s);
	exit(EXIT_FAILURE);
}
